<template>
  <el-dialog :visible.sync="dialog" :title="isAdd ? '新增测试单表' : '编辑测试单表'" append-to-body width="600px">
    <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" type="text" maxlength="20" style="width:460px" show-word-limit />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio v-for="item in dictobj['sex_status']" :key="item.value" v-model="form.sex" :label="item.value">{{ item.label }}</el-radio>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input-number v-model="form.age" controls-position="right" style="width:460px" />
      </el-form-item>
      <el-form-item label="出生日期" prop="birthday">
        <el-date-picker
          v-model="form.birthday"
          type="datetime"
          placeholder="选择日期时间"
          default-time="12:00:00"
          style="width:460px"
        />
      </el-form-item>
      <el-form-item label="服务类型" prop="serveiceType">
        <el-select v-model="form.serveiceType" clearable placeholder="请选择" style="width:460px">
          <el-option
            v-for="item in dictobj['services_status']"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="测试swich" prop="swich">
        <el-switch
          v-model="form.swich"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-checkbox-group v-model="form.userType" style="width:460px">
          <el-checkbox v-for="item in dictobj['user_type_status']" :key="item.value" :label="item.value">{{ item.label }}</el-checkbox>
        </el-checkbox-group>

      </el-form-item>
      <el-form-item label="应用类型" prop="cleintType">
        <el-select v-model="form.cleintType" clearable placeholder="请选择" style="width:460px">
          <el-option
            v-for="item in dictobj['clients_status']"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="form.remark" type="textarea" maxlength="255" style="width:460px" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/generator/testDb/index'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },

    dictobj: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      dialog: false, loading: false, form: {},
      userTypes: [],
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],

        birthday: [{ required: true, message: '出生日期不能为空', trigger: 'blur' }],

        userType: [{ type: 'array', required: true, message: '至少选择一个用户类型', trigger: 'change' }],

        cleintType: [{ required: true, message: '应用类型不能为空', trigger: 'blur' }]

      }
    }
  },
  created() {
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.userType = this.form.userType.join(',')
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          return false
        }
      })
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },

    resetForm() {
      this.dialog = false
      if (this.$refs['form']) {
        this.$refs['form'].resetFields()
      }
      this.form = {
        name: null,
        sex: null,
        age: null,
        birthday: null,
        serveiceType: null,
        swich: null,
        userType: [],
        cleintType: null,
        remark: null,
        id: 0
      }
    }
  }
}
</script>

<style scoped>

</style>
